package com.decent.ejfadmin.configure.controller;

import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.SupConfig;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.configure.service.ConfigureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 配置
 *
 * @author zhangchao
 * @date 2018/7/29 14:55
 */
@Slf4j
@RestController
@RequestMapping("config")
public class ConfigureController {

    @Autowired
    private ConfigureService configureService;

    /**
     * 更新配置value
     *
     * @param configureId
     * @param value
     * @return
     */
    @RequestMapping(value = "updateConfigValue", method = RequestMethod.POST)
    public MessageBean updateConfigValue(Long configureId, String value) {
        MessageBean messageBean = MessageBean.success();
        try {
            configureService.updateConfigValue(configureId, value);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ConfigureController updateConfigValue occurs error " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ConfigureController updateConfigValue occurs error Internal Server Error  ", e);
        }
        return messageBean;
    }

    /**
     * 查询配置信息
     *
     * @return
     */
    @RequestMapping(value = "findConfigInfo", method = RequestMethod.GET)
    public List<SupConfig> findConfigInfo() {
        try {
            return configureService.findConfigInfo();
        } catch (Exception e) {
            log.warn("ConfigureController findConfigInfo occurs error Internal Server Error  ", e);
            return Collections.emptyList();
        }
    }
}
